package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceimpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "新增菜品接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){

        log.info("新增菜品");

        dishService.insert(dishDTO);

        /*
         * 修改redis中的数据
         */
        String key = "dish_"+dishDTO.getCategoryId();
        cleanCache(key);


        return Result.success();

    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> queryByPage( DishPageQueryDTO dishPageQueryDTO){

        log.info("菜品分页查询：{}",dishPageQueryDTO);

        PageResult pageResult = dishService.queryByPage(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /*
    批量删除菜品 传入id集合（String类型）
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品：{}",ids);
        dishService.delete(ids);

        cleanCache("dish_*");

        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(("回显所要修改菜品得数据"))
    public Result<DishVO> getById(@PathVariable Long id ){

        log.info("回显菜品数据：{}",id);
        DishVO result = dishService.getDishData(id);

        return Result.success(result);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update( @RequestBody DishDTO dishDTO){

        log.info("修改菜品：{}",dishDTO);
        dishService.update(dishDTO);

        cleanCache("dish_*");

        return Result.success();
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list( Long categoryId){

        log.info("新增套餐中添加菜品所需要回显的数据-菜品id数据{}",categoryId);

        List<Dish> list = dishService.list(categoryId);

        return Result.success(list);

    }


    @PostMapping ("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result startOrstopDish( @PathVariable Integer status, Long id){


        dishService.startOrstopDish(id);

        //dishService.change(status,id);

        cleanCache("dish_*");

        return Result.success();
    }

    private void cleanCache(String pattern){

        //清理所有缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

    }




}
